﻿using UnityEngine;
using System.Collections;
namespace Room
{
    public delegate void LoadCompleteCallback();
    public class RCLODBase : RCComponent
    {
        public LODType type = LODType.None;

        public RCLODBase(RoomCharactor owner)
            : base(owner)
        {

        }

        #region model

        protected GameObject model;
        protected ModelRenderKit modelRenderKit;
        protected LegacyAnimation modelAnimation;

        public GameObject Model
        {
            get
            {
                return model;
            }
        }

        protected int level;//LODLevel

        private string loadingModelName;
        /// <summary>
        /// 加载model
        /// </summary>
        /// <param name="resourceName"></param>
        protected void LoadModel(string resourceName)
        {
            if (string.IsNullOrEmpty(resourceName))
            {
                return;
            }

            if (model != null)
            {
                UnloadModel();
            }
            loadingModelName = resourceName;
        }
        public override void Tick()
        {
            base.Tick();
            
            if (!string.IsNullOrEmpty(loadingModelName))
            {
                var asset = RoomModelPool.Instance.GetModelFromCache(loadingModelName);
                var obj = asset as GameObject;
                if (obj != null)
                {
                    //model的赋值只存在于这里和卸载model的地方
                    model = obj;
                    model.transform.SetParent(owner.transform);
                    model.transform.localPosition = Vector3.zero;
                    model.transform.localEulerAngles = Vector3.zero;
                    model.transform.localScale = Vector3.one;
                    model.SetActive(false);
                    model.name = loadingModelName;
                    modelAnimation = model.GetComponent<LegacyAnimation>();
                    modelRenderKit = model.GetComponent<ModelRenderKit>();
                    if (shadow)
                        ShowShadow();
                    loadingModelName = null;
                    //if (lifeUtil)
                    //    ShowLifeUtil();

                    OnLoadModelComplete();
                }
                //else {
                //    Debug.LogError("[RCLODBase] Load Model Failed. modelName:" + resourceName);
                //}
            }
        }
        /// <summary>
        /// 卸载model
        /// </summary>
        protected virtual void UnloadModel() {
            if(model!=null)
            {
                //model的赋值只存在于这里和Tick中加载成功的地方
                RoomModelPool.Instance.PutModelToCache(model.name, model);
                model = null;
                modelAnimation = null;
                modelRenderKit = null;
            }
            loadingModelName = null;
        }

        protected virtual void OnLoadModelComplete()
        {

        }
        
        public bool IsLoading()
        {
            return (!string.IsNullOrEmpty(loadingModelName));
        }
        
        public virtual void SetInView(bool inView)
        {

        }
        public virtual bool GetInView()
        {
            return true;
        }
        #endregion

        #region Animation
        protected string animName;
        protected WrapMode wrapMode;
        protected bool preAnim;
        protected bool firstPlay = true;

        public virtual void PlayAnim(string animName, WrapMode wrapMode)
        {
            this.animName = animName;
            this.wrapMode = wrapMode;
            if (modelAnimation != null)
            {
                if (firstPlay)
                {
                    firstPlay = false;
                    modelAnimation.CrossFade(animName, wrapMode, false, 0f);
                }
                else {
                    modelAnimation.CrossFade(animName, wrapMode, false, 0.1f);
                }
                //Debug.LogFormat("[RCLODBase]modelName:{0} animName:{1} duration:{2} ", model.name, aniName, modelAnimation.anim[aniName].length);
                preAnim = false;
            }
            else
            {
                preAnim = true;
            }
        }

        public virtual void StopAnim() {
            this.animName = "";
            if (modelAnimation != null)
            {
                modelAnimation.Stop();
            }
        }

        public virtual void ResumeAnim() {
            if (!string.IsNullOrEmpty(animName))
            {
                firstPlay = true;
                PlayAnim(animName, wrapMode);
            }
        }

        public virtual bool IsPlayingAnim(string an) {
            if (modelAnimation != null)
            {
                return modelAnimation.IsPlaying(an);
            }
            else
            {
                return preAnim && animName.Equals(an);
            }
        }

        public virtual bool isPlayingAnim {
            get {
                if (modelAnimation != null)
                {
                    return modelAnimation.isPlaying;
                }
                return preAnim;
            }
        }

        #endregion Animation

        #region shadow

        private bool shadow = false;

        protected bool IsShadowShowed() {
            return shadow;
        }

        protected void ShowShadow()
        {
            shadow = true;
            if(model!=null)
            {
                ModelRenderKit modelRenderKit = model.GetComponent<ModelRenderKit>();
                if (modelRenderKit != null)
                    modelRenderKit.ShowSimpleShadow(simpleShadowTex);
            }
        }

        protected void HideShadow()
        {
            shadow = false;
            if (model != null)
            {
                ModelRenderKit modelRenderKit = model.GetComponent<ModelRenderKit>();
                if (modelRenderKit != null)
                    modelRenderKit.HideSimpleShadow();
            }
        }

		private static GameObject _simpleShadowTex;
		protected static GameObject simpleShadowTex
        {
            get
            {
                if (_simpleShadowTex == null)
                {
					_simpleShadowTex = Resources.Load<GameObject>("texture/QuadShadow");
                }
                return _simpleShadowTex;
            }
        }

        #endregion

        #region level

        public virtual int GetLevel()
        {
            return level;
        }

        public virtual void SetLevel(int lodLevel)
        {
            if (lodLevel != level)
            {
                this.level = lodLevel;
            }
        }

        #endregion

        #region Importance

        public virtual int GetImportance()
        {
            return 0;
        }

        public virtual void SetImportance(int importance)
        {

        }

        #endregion

        public override void DoResume()
        {
            base.DoResume();
        }
        public override void DoPause()
        {
            base.DoPause();
            firstPlay = true;
        }

        //private bool lifeUtil = false;
        /*public virtual void ShowLifeUtil()
        {
            lifeUtil = true;
            if (model != null)
            {
                ModelRenderKit modelRenderKit = model.GetComponent<ModelRenderKit>();
                if (modelRenderKit != null)
                    modelRenderKit.LoadLifeUtil();
            }
        }*/

        

        public virtual float GetHeight()
        {
            return 0f;
        }

        public virtual void PlayParticle(string resName)
        {

        }
    }
}

